package org.hsc.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.hsc.model.Article;
import org.hsc.service.ArticleService;
import org.hsc.web.vo.NvgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping("/")
public class ArticleController {

	@Autowired ArticleService service;
	
	private List<NvgVo> getNvgList(){
		List<Article> artList=service.getNvg();
		Map<Long,List<NvgVo>> nvgMap=new HashMap<Long,List<NvgVo>>();
		List<NvgVo> nvgList=new ArrayList<NvgVo>();
		
		for(Article art:artList){
			NvgVo tmp=new NvgVo();
			tmp.setId(art.getId());
			tmp.setTitle(art.getTitle());
			tmp.setParent(art.getParent());
//			tmp.setChildren(new ArrayList<NvgVo>());
			nvgList.add(tmp);
		}
		for(NvgVo cvo:nvgList){
			List<NvgVo> vos=nvgMap.get(cvo.getParent());
			if(vos ==null){
				vos=new ArrayList<NvgVo>();
				nvgMap.put(cvo.getParent(), vos);
			}
			vos.add(cvo);
		}
		
		List<NvgVo> descNvgList=new ArrayList<NvgVo>();
		
		for(NvgVo vo:nvgList){
			List<NvgVo> vos=nvgMap.get(vo.getId());
			if(vos!=null){
				descNvgList.add(vo);
				vo.setChildren(vos);
			}else if(vo.getParent()==null){
				descNvgList.add(vo);
			}
		}
		return descNvgList;
	}
	
	@RequestMapping(value="/{id}")
	@ResponseBody
	public ModelAndView gotoArticle(@PathVariable(value="id") Long id){
		ModelAndView mv= new ModelAndView("/index.jsp");
		
		List<NvgVo> descNvgList = getNvgList();
		mv.addObject("nvgList",descNvgList);
		mv.addObject("art", service.get(id!=null?id:descNvgList.get(0).getId()));
		
		return mv;
	}
	
	
	@RequestMapping(value="/add.do")
	@ResponseBody
	public ModelAndView addArticle(
			@RequestParam(value="title",required=true) String title,
			@RequestParam(value="content",required=true) String content,
			@RequestParam(value="parent",required=false) Long parent
			){
		
		Article art=new Article();
		art.setTitle(title);
		art.setContent(content);
		art.setParent(parent);
		art.setCreateTime(new Date());
		art.setUpdateTime(new Date());
		
		service.add(art);
		
		ModelAndView mv= new ModelAndView("/index.jsp");
		
		List<NvgVo> descNvgList = getNvgList();
		mv.addObject("nvgList",descNvgList);
		mv.addObject("art", service.get(art.getId()));
		
		return mv;
	}
	@RequestMapping(value="/gotoAdd.do")
	@ResponseBody
	public ModelAndView gotoAdd(HttpServletRequest req){
		
		ModelAndView mv= new ModelAndView("/edit.jsp");
		
		List<NvgVo> descNvgList = getNvgList();
		mv.addObject("nvgList",descNvgList);
		
		return mv;
	}
	
	
	@RequestMapping(value="/update.do")
	@ResponseBody
	public ReturnResult updateArticle(
			@RequestParam(value="id",required=true) Long id,
			@RequestParam(value="title",required=false) String title,
			@RequestParam(value="content",required=false) String content,
			@RequestParam(value="parent",required=false) Long parent){
		
		Article art=service.get(id);
		art.setTitle(title);
		art.setContent(content);
		art.setParent(parent);
		art.setCreateTime(new Date());
		art.setUpdateTime(new Date());
		
		service.update(art);
		return ReturnResult.SUCCESS("更新成功！");
	}
	
	@RequestMapping(value="{id}/delete.do")
	@ResponseBody
	public ReturnResult deleteArticle(@PathVariable(value="id") Long id,HttpServletRequest req){
		if(id==1){
			return ReturnResult.FAILUER("不能删除默认页面！");
		}
		service.delete(id);
		return ReturnResult.SUCCESS("删除页面成功！");
	}
	
	/**
	 * @param id
	 * @return
	 */
	@RequestMapping(value="{id}/gotoEdit.do")
	@ResponseBody
	public ModelAndView gotoEdit(@PathVariable(value="id") Long id){
		ModelAndView mv= new ModelAndView("/edit.jsp");
		
		List<NvgVo> descNvgList = getNvgList();
		mv.addObject("nvgList",descNvgList);
		mv.addObject("art", service.get(id!=null?id:0));
		
		return mv;
	}
	
	
	/**
	 * @param id
	 * @param title
	 * @param content
	 * @param req
	 * @return
	 */
	@RequestMapping(value="{id}/edit.do",method=RequestMethod.POST)
	@ResponseBody
	public RedirectView edit(
			@PathVariable(value="id") Long id,
			@RequestParam(value="parent") Long parent,
			@RequestParam(value="title") String title,
			@RequestParam(value="content") String content,
			HttpServletRequest req
		){
		
		Article art=service.get(id);
		art.setContent(content);
		art.setTitle(title);
		art.setParent(parent);
		service.update(art);
		
		return new RedirectView(req.getContextPath()+"/"+ id +".do");
	}
}
